package br.com.toscano.contpedido.persistence;

import java.util.List;

import javax.persistence.Query;

import br.gov.frameworkdemoiselle.stereotype.PersistenceController;
import br.gov.frameworkdemoiselle.template.JPACrud;

import br.com.toscano.contpedido.domain.LocalProducao;
import br.com.toscano.contpedido.domain.LocalProducao;

@PersistenceController
public class LocalProducaoDAO extends JPACrud<LocalProducao, Long> {

	private static final long serialVersionUID = 1L;
	
	public LocalProducao consultar(String descricao) {

		String hql = " from " + LocalProducao.class.getSimpleName()+" obj ";
		boolean jaTemRestricao = false;
		if (descricao !=  null && !"".equals(descricao)) {
			if(jaTemRestricao) {
				hql = hql + " and ";
			} else {
				hql = hql + " where ";
			}
			hql = hql + " obj.descricao = '" + descricao.trim() + "' ";
			jaTemRestricao = true;
		}

		Query q = createQuery(hql);

		List<LocalProducao> lista = q.getResultList();
		if (lista.size() > 0) {
			return lista.iterator().next();
		}
		return null;
	}
}
